package day02;

import java.util.Arrays;
import java.util.Stack;

/**
 * Created by Administrator on 2023/6/30 0030.
 */
public class StackDemo {
    public static void main(String[] args) {
        Stack<Double> stack = new Stack<>();
        String str = "1 2 + 3 4 + *";
        String[] tokens = str.split(" ");
        System.err.println(Arrays.asList(tokens));
        for (String token : tokens) {
            if (checkDouble(token)) {
                stack.push(Double.parseDouble(token));

            } else {
                double b = stack.peek();
                stack.pop();
                double a = stack.peek();
                stack.pop();
                double c = 0;
                switch (token.charAt(0)) {
                    case '+':
                        c = a + b;
                        break;
                    case '*':
                        c = a * b;
                        break;
                    default:
                        break;
                }
                stack.push(c);

            }
            System.err.println(stack);
        }
        System.out.println(stack.peek());


    }

    private static boolean checkDouble(String token) {
        try {
            Double.parseDouble(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
